
// import store from '@/store/index.js'


// let baseUrl = 'http://118.24.234.156:9083/'
// let baseUrl = 'http://192.168.5.139:9083/'
let baseUrl = 'http://47.102.215.120:9083/'
//#ifdef H5
baseUrl = 'api/'
//#endif
let ajaxTimes = 0 // 页面发送请求的个数
export default function request(options){
	// console.log(store)
	ajaxTimes++;
	options.header = {
		'Content-Type':'application/json;charset=UTF-8'
	}
	// console.log(options.url.indexOf('login') == -1)
	if(uni.getStorageSync('authorization') && options.url.indexOf('login') == -1){//如果有登录token 则给token添加到请求头
		options.header['Authorization'] = uni.getStorageSync('authorization')
	}
	if(options.url.indexOf('http') == '-1'){
		options.url = baseUrl + options.url
	}
	options.data = options.data || {}
	return new Promise((resolve,reject) => {
		uni.showLoading({
		    title: '加载中'
		});
		
		uni.request({
			...options,
			// url: baseUrl + options.url,
			method:options.method || 'POST',
			success:(res)=>{	
				if(res.data.code == '401'){
					uni.redirectTo({
						url:'/pages/login/login'
					})
					// gameOver()
				}
				resolve(res.data)
			},
			fail:(error)=>{
				console.log(error)
				// gameOver()
				reject(error)
			},
			complete: () => {
				ajaxTimes--;
				if(ajaxTimes == 0){
					uni.hideLoading();
					uni.stopPullDownRefresh();
				}
			}
		})
	});
}

function gameOver(){
	console.log('登录超时了.....')
	let pages = getCurrentPages()
	let backPage = pages[pages.length - 1].route
	console.log(backPage)
	let backType = 1 // 1 navigateTo 2 switchTab
	if(backPage.indexOf('/home/home') != -1 || backPage.indexOf('/orderManger/orderManger') != -1 || backPage.indexOf('/analysis/analysis') != -1 || backPage.indexOf('/mine/mine') != -1){
		backType = 2
	}
	uni.redirectTo({
		url:`/pages/login/login?backPage=${backPage}&backType=${backType}`
	})
	return;
}